<?php
class ModelFAGroup extends Model {
	public function addGroup($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "fa_group SET sort_order = '" . (int)$data['sort_order'] . "', status = '" . (int)$data['status'] . "'");

		$group_id = $this->db->getLastId();
				
		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "fa_group SET image = '" . $this->db->escape(html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8')) . "' WHERE group_id = '" . (int)$group_id . "'");
		}
		
		foreach ($data['group_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "fa_group_description SET group_id = '" . (int)$group_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', description = '" . $this->db->escape($value['description']) . "'");
		}

		$this->cache->delete('group');
	}
	
	public function editGroup($group_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "fa_group SET sort_order = '" . (int)$data['sort_order'] . "', status = '" . (int)$data['status'] . "' WHERE group_id = '" . (int)$group_id . "'");

		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "fa_group SET image = '" . $this->db->escape(html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8')) . "' WHERE group_id = '" . (int)$group_id . "'");
		}

		$this->db->query("DELETE FROM " . DB_PREFIX . "fa_group_description WHERE group_id = '" . (int)$group_id . "'");

		foreach ($data['group_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "fa_group_description SET group_id = '" . (int)$group_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', description = '" . $this->db->escape($value['description']) . "'");
		}
				
		$this->cache->delete('group');
	}
	
	public function deleteGroup($group_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "fa_group WHERE group_id = '" . (int)$group_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "fa_group_description WHERE group_id = '" . (int)$group_id . "'");
		
		$this->cache->delete('group');
	} 
	
	public function getGroup($group_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "fa_group c LEFT JOIN " . DB_PREFIX . "fa_group_description cd ON (c.group_id = cd.group_id )");
		
		return $query->row;
	} 
	
	public function getGroups($data) {
		$sql = "SELECT * FROM " . DB_PREFIX . "fa_group c LEFT JOIN " . DB_PREFIX . "fa_group_description cd ON (c.group_id = cd.group_id) WHERE cd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
			
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}				

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
		 
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
						
		$query = $this->db->query($sql);
		
		return $query->rows;
	}
				
	public function getGroupDescriptions($group_id) {
		$group_description_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "fa_group_description WHERE group_id = '" . (int)$group_id . "'");
		
		foreach ($query->rows as $result) {
			$group_description_data[$result['language_id']] = array(
				'name'             => $result['name'],
				'description'      => $result['description']
			);
		}
		
		return $group_description_data;
	}	
		
	public function getTotalGroups() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "fa_group");
		
		return $query->row['total'];
	}	
		
	public function getTotalGroupsByImageId($image_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "fa_group WHERE image_id = '" . (int)$image_id . "'");
		
		return $query->row['total'];
	}
}
?>